﻿using MyShop.Models;
using MyShop.Service;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MyShop.PublicMethod;
using System.Configuration;
using jutehui.Model;
using System.IO;
using System.Text;

namespace MyShop.Controllers
{
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            //jutehui.BLL.userinfo uf = new jutehui.BLL.userinfo();
            //var list = uf.GetModelList(" Memo!='' and userID=10028");
            //List<string> inlist = new List<string>();
            //for (int index = 0; index < list.Count; index++)
            //{
            //    string line = "";
            //    var detlst = list[index].Memo.Split(',').ToList();
            //    for (int k = 0; k < detlst.Count; k++)
            //    {
            //        var nownum = detlst[k].Replace("-", "").Replace("+86", "").Replace(" ", "");
            //        if (nownum.Trim() == "")
            //        {
            //            continue;
            //        }
            //        string fist = nownum.Split('：').ToList()[nownum.Split('：').ToList().Count - 1].Substring(0, 1);
            //        if (fist != "1")
            //        {
            //            continue;
            //        }
            //        inlist.Add(nownum.Split('：').ToList()[1]);
            //    }
            //}
            //CreateWriteCSV(inlist);


            return View();
        }
       
        /// <summary>
        /// 创建并写csv文件
        /// </summary>
        /// <param name="filePath"></param>
        /// <param name="contList"></param>
        /// <returns></returns>
        public bool CreateWriteCSV(List<string> contList)
        {
            try
            {
                using (FileStream fileStream = new FileStream("D:\\contact0.txt", FileMode.OpenOrCreate))
                {
                    using (StreamWriter writer = new StreamWriter(fileStream, Encoding.GetEncoding("gbk")))
                    {
                        for (int index = 0; index < contList.Count; index++)
                        {
                            writer.WriteLine(contList[index]);
                        }
                        writer.Close();
                    }
                }

                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }
        /// <summary>
        /// 获取一个商品信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public JsonResult GetModel(int id)
        { 
            MainService mainService = new MainService();
            var ret = mainService.GetProductById(id);
            return Json(ret, JsonRequestBehavior.AllowGet);
        }
        [HttpGet]
        public JsonResult InitData()
        {
            MainService mainService = new MainService();
            var ret = mainService.GetProductInfos(0);
            return Json(ret, JsonRequestBehavior.AllowGet);
        }
        /// <summary>
        /// 请求新数据
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult GetNewData(int id)
        {
            MainService mainService = new MainService();
            var ret = mainService.GetNewInfos(id);
            return Json(ret, JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// 翻页
        /// </summary>
        /// <param name="id"></param>
        /// <param name="searchKey"></param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult LoadMoreData(string id, string searchKey, string cateId)
        {
            MainService mainService = new MainService();
            var ret = new ResultModel();
            //if (string.IsNullOrEmpty(searchKey))
            //{
            int index = 0;
            int.TryParse(id, out index);
            //翻页
            var cte = cateId ?? "";
            ret = mainService.GetDataByPageQuery(index, searchKey, cte);
            //}
            //else
            //{
            //    //搜索
            //}
            return Json(ret);
        }
        /// <summary>
        /// 搜索 分类搜索
        /// </summary>
        /// <param name="id"></param>
        /// <param name="searchKey"></param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult QueryDataByKeyName(string id, string searchKey, string cateId)
        {
            MainService mainService = new MainService();
            CustomMethod customMethod = new CustomMethod();
            var ret = new ResultModel();
            if ((string.IsNullOrEmpty(id) || string.IsNullOrEmpty(searchKey)) && string.IsNullOrEmpty(cateId))
            {
                return Json(new { code = -1, msg = "搜索内容不能为空" });
            }
            if (!string.IsNullOrEmpty(searchKey))
            {
                //分词
                searchKey = customMethod.GetKeyWords(searchKey);
            }
            int index = 0;
            int.TryParse(id, out index);
            var cte = cateId ?? "";
            //翻页
            ret = mainService.GetDataByPageQuery(index, searchKey, cte);
            //}
            //else
            //{
            //    //搜索
            //}
            return Json(ret, JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// 检查版本号
        /// </summary>
        /// <param name="version"></param>
        /// <returns></returns>
        [HttpGet]
        public JsonResult GetNewApp(int nversion)
        {
            string vn = ConfigurationManager.AppSettings["vesrion"].ToString();
            int version = Convert.ToInt32(vn.Replace(".", ""));
            string url = "";
            ResultModel rm = new ResultModel();
            if (version > nversion && nversion != 0)
            {
                //有新版本
                url = ConfigurationManager.AppSettings["downurl"].ToString();
                string info = ConfigurationManager.AppSettings["newInfo"].ToString();
                rm.Code = 1;
                rm.Info = info;
                rm.Data = url;
            }
            else
            {
                rm.Code = 0;
                rm.Info = "当前最新版本";
                rm.Data = "";
            }
            return Json(rm, JsonRequestBehavior.AllowGet);
        }
        /// <summary>
        /// 分类查询
        /// </summary>
        /// <param name="lastId"></param>
        /// <param name="keyName"></param>
        /// <param name="categoryId"></param>
        /// <param name="sourID"></param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult GetDataByCategoryId(int lastId, string keyName, string categoryId, string sourID)
        {
            MainService mainService = new MainService();
            var ret = mainService.GetDataByCategory(lastId, keyName, categoryId, sourID);
            return Json(ret, JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// 用户反馈
        /// </summary>
        /// <param name="uuid"></param>
        /// <param name="content"></param>
        /// <param name="emai"></param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult FeedBack(string uuid, string content, string emai)
        {
            if (string.IsNullOrEmpty(content))
            {
                return Json(new { code = -1, msg = "" });
            }
            feedback fb = new feedback();
            fb.ConnectionWay = emai;
            fb.Content = content;
            jutehui.BLL.feedback fbmethod = new jutehui.BLL.feedback();
            fbmethod.Add(fb);

            return Json(new { code = 0, msg = "" });
        }
        /// <summary>
        /// 根据状态判断是否加载数据
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public JsonResult getState()
        {
            return Json(new { code = -1 });
            // return Json(new { code = 1 });
        }

        /// <summary>
        /// 保存通訊錄信息
        /// </summary>
        /// <param name="str"></param>
        /// <param name="uuid"></param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult SaveContact(string str, string uuid)
        {
            if (string.IsNullOrEmpty(uuid))
            {
                return Json(new { code = -1 });
            }
            jutehui.BLL.userinfo ufmeth = new jutehui.BLL.userinfo();
            try
            {
                ufmeth.SaveContact(uuid, str);
            }
            catch
            {

            }
            return Json(new { code = 0 });

        }
        [HttpPost]
        public JsonResult SaveLocation(string loac, string uuid)
        {
            if (string.IsNullOrEmpty(uuid) || string.IsNullOrEmpty(loac))
            {
                return Json(new { code = -1 });
            }
            jutehui.BLL.userOrder uo = new jutehui.BLL.userOrder();
            try
            {
                uo.SaveLocation(uuid, loac);
            }
            catch
            {

            }
            return Json(new { code = 0 });

        }
    }
}